﻿<cfsilent>
	<cfscript>
	
		sql = "	SELECT
					c.campus_name
				FROM
					t_campus c
				WHERE
					c.campus_id = :campusId ";
		
		queryObj = new Query(datasource = application.dnsSlave);
		queryObj.addParam(name = "campusId", value = event.getArg("CID"), cfsqltype = "cf_sql_char");
		rs_campusEntity = queryObj.execute( sql = sql ).getResult();

		sql = "	SELECT
					b.b_id,
					b.b_name
				FROM
					t_building b
				WHERE
					b.campus_id = :campusId 
				ORDER BY
					b.b_id ASC";
		
		queryObj = new Query(datasource = application.dnsSlave);
		queryObj.addParam(name = "campusId", value = event.getArg("CID"), cfsqltype = "cf_sql_char");
		rs_build = queryObj.execute( sql = sql ).getResult();
		
		dictAdvice = getProperty("serviceFactory").getBean("senateDictionaryAdvice");
		usageAdvice = getProperty("serviceFactory").getBean("classroomUsageAdvice");
		
		temFile = GetTempDirectory() & createUUID() & ".xls";
		downFile = URLEncodedFormat(rs_campusEntity.campus_name & "教学场地信息", "utf-8");
		
		excel = getProperty("serviceFactory").getBean("spreadSheetObject");
		
		/* 创建工作簿 */
		spreadsheetObj = excel.SpreadSheetNew("楼宇信息");
		
		/* 添加表头 */
		excel.SpreadsheetAddrow(spreadsheetObj, "教学楼,教学场地数量");
		
		for(a = 1; a lte rs_build.recordCount; a++){
			
			/* 按照教学楼依次循环 */
			sql = "	SELECT
						cr.cr_usage,
						cr.classroom,
						ct.cr_type_name,
						cr.cr_floor,
						cr.sits,
						cr.sits_course,
						cr.sits_exam
					FROM
						t_classroom cr
						INNER JOIN t_classroom_type ct ON ct.cr_type = cr.cr_type
					WHERE
						cr.b_id = :buildingId 
					ORDER BY
						cr.cr_floor ASC,cr.classroom ASC";
			
			queryObj = new Query(datasource = application.dnsSlave);
			queryObj.addParam(name = "buildingId", value = rs_build['b_id'][a], cfsqltype = "cf_sql_char");
			rs_clsroom = queryObj.execute( sql = sql ).getResult();
			
			/* 汇总信息设置为当前写入工作区 */
			excel.SpreadsheetSetActiveSheet (spreadsheetObj, "楼宇信息");
			
			/* 构建楼宇信息 */
			excel.SpreadsheetAddrow(spreadsheetObj, rs_build['b_name'][a] & "," & rs_clsroom.recordCount);
			
			/* 创建楼宇详细信息工作区 */
			excel.SpreadsheetCreateSheet (spreadsheetObj, rs_build['b_name'][a]);
			excel.SpreadsheetSetActiveSheet (spreadsheetObj, rs_build['b_name'][a]);
			
			row = 1;
			
			/* 添加表头 */
			excel.SpreadsheetAddrow(spreadsheetObj, "教学场地,教室类别,所在楼层,座位数,排课容量,考试容量,是否可用,排课,考试,日常借用,自习,教学楼");
			
			for(b = 1; b lte rs_clsroom.recordCount; b++){
				
				usageAdvice.parseUsage( rs_clsroom['cr_usage'][b] );
				
				row++;
				
				/* 构建教室信息 */
				rowData = rs_clsroom['classroom'][b]
						& ","
						& rs_clsroom['cr_type_name'][b]
						& ","
						& dictAdvice.getFloorString(rs_clsroom['cr_floor'][b])
						& ","
						& rs_clsroom['sits'][b]
						& ","
						& rs_clsroom['sits_course'][b]
						& ","
						& rs_clsroom['sits_exam'][b]
						& ","
						& usageAdvice.getRunning()
						& ","
						& usageAdvice.getForCourse()
						& ","
						& usageAdvice.getForExam()
						& ","
						& usageAdvice.getForBorrow()
						& ","
						& usageAdvice.getForStudy()
						& ","
						& rs_build['b_name'][a];
				
				/* 写入教室信息 */
				excel.SpreadsheetAddrow(spreadsheetObj, rowData);
				
			}
			
		}
		
		excel.SpreadsheetSetActiveSheet (spreadsheetObj, "楼宇信息");
		
		excel.SpreadSheetWrite(spreadsheetObj, temFile, true);
		
	</cfscript>

	<cfheader name="Content-Disposition" value="attachment; filename=#downFile#.xls" />
	<cfcontent file="#temFile#" reset="yes" type="application/msexcel" deletefile="yes" />	
	
</cfsilent>
